package com.jd.mysql.mgr.controller.mgrdberror;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.jd.mysql.mgr.dto.MgrDbErrorDTO;
import com.jd.mysql.mgr.pojo.MgrAccountGroup;
import com.jd.mysql.mgr.pojo.MgrDbError;
import com.jd.mysql.mgr.pojo.MgrMachine;
import com.jd.mysql.mgr.service.IMgrAccountService;
import com.jd.mysql.mgr.service.IMgrDbErrorService;
import com.jd.mysql.mgr.service.IMgrMachineService;
import com.jd.mysql.mgr.util.DateEditor;
import com.jd.mysql.mgr.util.GlobalStaticVariables;
import com.jd.mysql.mgr.util.PageUtil;
import com.jd.mysql.mgr.util.ParamUtil;
import com.jd.mysql.mgr.util.StringUtil;

public class AddMgrDbErrorController extends SimpleFormController {
	
	private IMgrDbErrorService mgrDbErrorService;
	private IMgrMachineService mgrMachineService;
	private IMgrAccountService mgrAccountService;
	
	

	public void setMgrAccountService(IMgrAccountService mgrAccountService) {
		this.mgrAccountService = mgrAccountService;
	}

	public void setMgrMachineService(IMgrMachineService mgrMachineService) {
		this.mgrMachineService = mgrMachineService;
	}

	public void setMgrDbErrorService(IMgrDbErrorService mgrDbErrorService) {
		this.mgrDbErrorService = mgrDbErrorService;
	}

	public AddMgrDbErrorController() {
		this.setCommandClass(MgrDbError.class);
	}
	
	
	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
		       binder.registerCustomEditor(Date.class,new DateEditor());
	} 

	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		// TODO Auto-generated method stub
		MgrDbError dberror = (MgrDbError)command;
		HttpSession session = request.getSession(true);
		Integer dbtype = (Integer)session.getAttribute("group");
		MgrAccountGroup group = mgrAccountService.findMgrAccountGroupById(dbtype);
		if("all".equals(group.getMgrGroupName())){
			dbtype = -1;
		}
		Integer id = ParamUtil.getIntParam(request, "id", 0) ;
		MgrMachine machine = null;
		if(StringUtil.isNull(dberror.getIp())){
			request.setAttribute("msg", "ip is null");
	        return new ModelAndView("error");
		}else{
			if(group.getMgrGroupName()=="mysql"){
				machine = mgrMachineService.getMgrMachineByIp(dberror.getIp());
				if(machine==null){
					request.setAttribute("msg", "machine is not exits!<a href='redirect.do?method=addmachine'>添加机器信息</a>");
			        return new ModelAndView("error");
				}
			}
		}
		MgrDbError olddberror = mgrDbErrorService.findMgrDbErrorById(id);
		if(olddberror!=null){
			olddberror.setAnalysis(dberror.getAnalysis());
			olddberror.setApplyperson(dberror.getApplyperson());
			olddberror.setDbname(dberror.getDbname());
			olddberror.setDbtype(dberror.getDbtype());
			olddberror.setDisposalmethod(dberror.getDisposalmethod());
			olddberror.setEmergencyplan(dberror.getEmergencyplan());
			olddberror.setFaultdate(dberror.getFaultdate());
			if(group.getMgrGroupName()=="mysql"){
				olddberror.setHostname(machine.getHostName());
			}else{
				olddberror.setHostname(dberror.getHostname());
			}
			olddberror.setPhenomenon(dberror.getPhenomenon());
			olddberror.setIp(dberror.getIp());
			mgrDbErrorService.updateMgrDbError(olddberror);
		}else{
			if(group.getMgrGroupName()=="mysql"){
				dberror.setHostname(machine.getHostName());
			}
			mgrDbErrorService.saveMgrDbError(dberror);
		}
		String index = request.getParameter("index");
		Map<String,Object> map = new HashMap<String,Object>();
		Integer start = PageUtil.setPageParam(map,index, GlobalStaticVariables.limit);
		
		
		Long totalCount = mgrDbErrorService.getMgrDbErrorCount(dbtype);
		List<MgrDbError> dtoList = mgrDbErrorService.getMgrDbErrorList(dbtype, start, GlobalStaticVariables.limit);
		List<MgrDbErrorDTO> list = new ArrayList<MgrDbErrorDTO>();
		for(MgrDbError error : dtoList){
			list.add(error.toDTO());
		}
		map.put("totalCount", totalCount);
		map.put("dtoList",list);
		return new ModelAndView("dberror/dberrorList","map",map);
	}
	
}
